using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace DrivingGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Dashboard : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch dashboardSpriteBatch;
        SpriteFont dashboardFont;
        Texture2D shieldIcon;
        Texture2D speedIcon;
        Texture2D bombIcon;

        Texture2D mapTexture;

        int shield = 0;
        int speed = 0;
        int bomb = 0;
        int health = 100;

        ContentManager Content;
        private int[,] floorPlan = new int[,]
        {
            {1,2,3,5,2,1,4,3,1,3,2,4,5,1,2},
            {2,0,0,0,0,0,0,0,0,0,0,0,0,0,3},
            {5,0,3,2,1,3,4,5,2,5,4,2,1,0,4},
            {3,0,3,0,0,0,0,0,0,0,0,0,4,0,3},
            {5,0,4,0,2,1,2,3,5,4,3,0,2,0,1},
            {3,0,2,0,4,0,0,0,0,0,2,0,3,0,4},
            {4,0,5,0,3,0,3,2,5,0,5,0,3,0,3},
            {1,0,4,0,4,0,1,0,0,0,4,0,1,0,2},
            {2,0,2,0,1,0,2,1,3,5,1,0,3,0,5},
            {4,0,3,0,5,0,0,0,0,0,0,0,2,0,3},
            {1,0,5,0,2,1,2,1,3,5,2,1,5,0,1},
            {4,0,4,0,0,0,0,0,0,0,0,0,0,0,2},
            {3,0,4,2,3,1,2,3,5,4,1,3,5,2,4},
            {2,0,0,0,0,0,0,0,0,0,0,0,0,0,4},
            {5,1,2,2,4,3,3,4,5,2,5,1,1,3,5}
        };

        public Dashboard(Game game, ContentManager content)
            : base(game) 
        {
            // TODO: Construct any child components here
            Content = content;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            base.Initialize();
        }

        protected override void LoadContent()
        {
            dashboardSpriteBatch = new SpriteBatch(Game.GraphicsDevice);
            dashboardFont = Game.Content.Load<SpriteFont>("dashboardFont");

            shieldIcon = Content.Load<Texture2D>("Icon//shield");
            speedIcon = Content.Load<Texture2D>("Icon//speed");
            bombIcon = Content.Load<Texture2D>("Icon//bomb");

            int cityWidth = floorPlan.GetLength(0);
            int cityLength = floorPlan.GetLength(1);

            mapTexture = new Texture2D(Game.GraphicsDevice, cityWidth, cityLength);
            Color[] mapColorData = new Color[cityWidth * cityLength];
            //for (int i = 0; i < mapColorData.Length; ++i)
            //{
            //    int row;
            //    int index;
            //    if (i > 14)
            //    {
            //        row = i % cityWidth-1;
            //        index = i - (row * cityWidth-1);
            //    }
            //    else
            //    {
            //        row = 0;
            //        index = i;
            //    }
                
            //    if (floorPlan[row, index] == 0)
            //    {
            //        mapColorData[i] = Color.Chocolate;
            //    }
            //    else
            //    {
            //        mapColorData[i] = Color.Blue;
            //    }
            //}
            mapTexture.SetData(mapColorData);
            
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public void addShield()
        {
            shield += 1;
        }

        public void useShield()
        {

        }

        public void addBomb()
        {
            bomb += 1;
        }

        public void useBomb()
        {
            bomb -= 1;
        }

        public void addSpeed()
        {
            speed += 1;
        }

        public void useSpeed()
        {
            speed -= 1;
        }

        public void DrawIcons(int screenWidth)
        {
            dashboardSpriteBatch.Begin();

            Vector2 shieldPosition = new Vector2(screenWidth - 100, 50);
            Vector2 speedPosition = new Vector2(screenWidth - 100, 200);
            Vector2 bombPosition = new Vector2(screenWidth - 100, 350);

            Vector2 shieldTextPosition = new Vector2(screenWidth - 75, 120);
            Vector2 speedTextPosition = new Vector2(screenWidth - 75, 270);
            Vector2 bombTextPosition = new Vector2(screenWidth - 75, 420);

            dashboardSpriteBatch.Draw(shieldIcon, shieldPosition, Color.White);
            dashboardSpriteBatch.Draw(speedIcon, speedPosition, Color.White);
            dashboardSpriteBatch.Draw(bombIcon, bombPosition, Color.White);

            dashboardSpriteBatch.DrawString(dashboardFont, Convert.ToString(shield), shieldTextPosition, Color.Snow);
            dashboardSpriteBatch.DrawString(dashboardFont, Convert.ToString(speed), speedTextPosition, Color.Snow);
            dashboardSpriteBatch.DrawString(dashboardFont, Convert.ToString(bomb), bombTextPosition, Color.Snow);

            DrawMiniMap(screenWidth);

            dashboardSpriteBatch.End();
        }

        public void DrawMiniMap(int screenWidth)
        {
            Vector2 mapPosition = new Vector2(Convert.ToInt32(screenWidth * 0.05), 50);
            dashboardSpriteBatch.Draw(mapTexture, mapPosition, Color.White);
        }
    }
}
